package vip.specimen.system.controller.resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemResourceSuffixEntity;
import vip.specimen.system.datasource.validation.SystemResourceSuffixValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemResourceSuffixService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/10 15:06
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 * 文件后缀管理
 **/
@Api(tags = "资源后缀")
@RestController
@RequestMapping("/resource/suffix")
public class SuffixController {
    @Resource
    private SystemResourceSuffixService resourceSuffixService;

    @PutMapping
    @RequiresPermissions(value = "resource:suffix:insert")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加资源后缀")
    @ApiOperation(value = "新增", notes = "添加资源后缀")
    public JsonResult<SystemResourceSuffixEntity> insert(@RequestBody @Valid SystemResourceSuffixValidation suffixValidation) {
        return JsonResult.success(this.resourceSuffixService.insert(suffixValidation));
    }

    @GetMapping
    @RequiresPermissions(value = "resource:suffix:select")
    @ApiOperation(value = "查看", notes = "获取资源后缀列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看资源后缀")
    public JsonResult<List<SystemResourceSuffixEntity>> suffixAll() {
        return JsonResult.success(this.resourceSuffixService.findAll());
    }

    @GetMapping(value = "usable")
    @RequiresPermissions(value = "resource:suffix:select:usable")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看可用的资源后缀")
    @ApiOperation(value = "查看可用后缀", notes = "获取未被使用的后缀")
    public JsonResult<List<SystemResourceSuffixEntity>> suffixUsableAll() {
        return JsonResult.success(this.resourceSuffixService.findUsableAll());
    }

    @GetMapping(value = "current")
    @RequiresPermissions(value = "resource:suffix:select:current")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看当前分类的资源后缀")
    @ApiOperation(value = "查看当前分类的后缀集", notes = "获取当前分类的后缀集")
    public JsonResult<List<SystemResourceSuffixEntity>> suffixUsableAllAndCurrentFind(@RequestParam("uuid") @ApiParam(value = "资源分类ID") UUID uuid) {
        return JsonResult.success(this.resourceSuffixService.findSystemResourceSuffixEntityByKindEntity_UuidOrKindEntityIsNull(uuid));
    }

    @PostMapping(value = "enable")
    @RequiresPermissions(value = "resource:suffix:update:enable")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新资源后缀的状态")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新后缀状态")
    public JsonResult<SystemResourceSuffixEntity> update(@RequestParam("uuid") UUID uuid) {
        SystemResourceSuffixEntity result = this.resourceSuffixService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "resource:suffix:delete")
    @ApiOperation(value = "删除", notes = "根据uuid删除资源后缀")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除资源后缀")
    public JsonResult<SystemResourceSuffixEntity> delete(@RequestBody HashMap<String, UUID> param) {
        return this.resourceSuffixService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }
}
